﻿using Framework.Core.Enums;
using Framework.Ddd.Application.Contracts.Dtos;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using my.abp.Application.Contracts.Dtos.Role;
using my.abp.Application.Contracts.IServices.Role;
using my.abp.Domain.IManagers.Org;
using my.abp.Domain.IManagers.Role;
using my.abp.Domain.Managers.Role;
using my.abp.Domain.Shared.Entities;
using my.abp.Domain.Shared.Enums;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Uow;

namespace my.abp.Application.Services.Role
{
	public class SysRoleService : ApplicationService, ISysRoleService
	{
		private ISysRoleManager _sysRoleManager;
		private ISysRoleMenuManager _sysRoleMenuManager;
		private ISysRoleOrgManager _sysRoleOrgManager;

		public SysRoleService(ISysRoleManager sysRoleManager, ISysRoleMenuManager sysRoleMenuManager,
			ISysRoleOrgManager sysRoleOrgManager)
		{
			_sysRoleManager = sysRoleManager;
			_sysRoleMenuManager = sysRoleMenuManager;
			_sysRoleOrgManager = sysRoleOrgManager;
		}

		/// <summary>
		/// 获取角色分页列表
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[Authorize]
		[HttpPost]
		[DisplayName("获取角色分页列表")]
		public async Task<PagedResultDto<SysRole>> Page(PageRoleInput input)
		{
			RefAsync<int> total = 0;

			var outPut = await _sysRoleManager.ListAsync(input.Name, input.Code,
				input.SkipCount, input.MaxResultCount, total);

			var result = new PagedResultDto<SysRole>();
			result.Items = outPut;
			result.TotalCount = total;
			return result;
		}

		/// <summary>
		/// 获取角色列表
		/// </summary>
		/// <returns></returns>
		[Authorize]
		[DisplayName("获取角色列表")]
		public async Task<List<RoleOutput>> GetList()
		{
			var outPut = await _sysRoleManager.GetList();
			return await outPut.Select<RoleOutput>().ToListAsync();
		}

		/// <summary>
		/// 增加角色
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[Authorize]
		[HttpPost]
		[DisplayName("增加角色")]
		public async Task AddRole(AddRoleInput input)
		{
			await _sysRoleManager.AddRole(input.Adapt<SysRole>(), input.MenuIdList);
		}

		/// <summary>
		/// 更新角色
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[Authorize]
		[HttpPost]
		[DisplayName("更新角色")]
		public async Task UpdateRole(UpdateRoleInput input)
		{
			await _sysRoleManager.UpdateRole(input.Adapt<SysRole>(), input.MenuIdList);
		}

		/// <summary>
		/// 删除角色
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[Authorize]
		[HttpPost]
		[DisplayName("删除角色")]
		public async Task DeleteRole(DeleteRoleInput input)
		{
			await _sysRoleManager.DeleteRole(input.Adapt<SysRole>());
		}

		/// <summary>
		/// 授权角色数据范围
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[Authorize]
		[HttpPost]
		[DisplayName("授权角色数据范围")]
		public async Task GrantDataScope(RoleOrgInput input)
		{
			await _sysRoleManager.GrantDataScope(input.Id, input.DataScope,input.OrgIdList);
		}

		/// <summary>
		/// 根据角色Id获取菜单Id集合
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[Authorize]
		[DisplayName("根据角色Id获取菜单Id集合")]
		public async Task<List<long>> GetOwnMenuList([FromQuery] RoleInput input)
		{
			return await _sysRoleMenuManager.GetRoleMenuIdList(new List<long> { input.Id });
		}

		/// <summary>
		/// 根据角色Id获取机构Id集合
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[Authorize]
		[DisplayName("根据角色Id获取机构Id集合")]
		public async Task<List<long>> GetOwnOrgList([FromQuery] RoleInput input)
		{
			List<long> roleIdList = new List<long>();
			if (input.Id != null) roleIdList.Add(input.Id);
			return await _sysRoleOrgManager.GetRoleOrgIdList(roleIdList);
		}

		/// <summary>
		/// 设置角色状态
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[Authorize]
		[HttpPost]
		[DisplayName("设置角色状态")]
		public async Task<int> SetStatus(RoleInput input)
		{
			return await _sysRoleManager.SetStatus(input.Adapt<SysRole>());
		}
	}
}
